<?php
class ToolsModel {
	public function getMulitPage($iItem, $iPages, $iPage) {
		if ($iPages > 1) {
			$sUrl = './?' . Base::getSearch ( 'page' );
			$sMulitPage = '<form onsubmit="mulitpagesub(this);" id="mulitpageform" action="' . $sUrl . '" method="post">';
			$sMulitPage .= '<table class="mulitpage"><tr><td>共' . $iItem . '条</td><td>' . $iPage . '/' . $iPages . '</td>';
			if ($iPage > 1) {
				$sMulitPage .= '<td><a href="' . $sUrl . '">首页</a></td>';
				$sMulitPage .= '<td><a href="' . $sUrl . '&page=' . ($iPage - 1) . '">上页</a></td>';
			}
			if ($iPage < $iPages) {
				$sMulitPage .= '<td><a href="' . $sUrl . '&page=' . ($iPage + 1) . '">下页</a></td>';
				$sMulitPage .= '<td><a href="' . $sUrl . '&page=' . $iPages . '">尾页</a></td>';
			}
			$sMulitPage .= '<td>转到第<input type="text" style="width:20px" id="page">页</td></tr></table></form>';
			$sMulitPage .= '<script>
			function mulitpagesub(obj) {
				obj.action += "&page="+document.getElementById("page").value;
			}
			</script>';
		} else {
			$sMulitPage = '';
		}
		return $sMulitPage;
	}
	
	public function getMulitPagefront($iItem, $iPages, $iPage) {
		if ($iPages > 1) {
			$sUrl = $_SERVER ['PHP_SELF'] . '?' . Base::getSearch ( 'page' );
			$sMulitPage = '<form onsubmit="mulitpagesub(this);" id="mulitpageform" action="' . $sUrl . '" method="post">';
			$sMulitPage .= '<table class="mulitpage"><tr><td>共' . $iItem . '条</td><td>' . $iPage . '/' . $iPages . '</td>';
			if ($iPage > 1) {
				$sMulitPage .= '<td><a href="' . $sUrl . '">首页</a></td>';
				$sMulitPage .= '<td><a href="' . $sUrl . '&page=' . ($iPage - 1) . '">上页</a></td>';
			}
			if ($iPage < $iPages) {
				$sMulitPage .= '<td><a href="' . $sUrl . '&page=' . ($iPage + 1) . '">下页</a></td>';
				$sMulitPage .= '<td><a href="' . $sUrl . '&page=' . $iPages . '">尾页</a></td>';
			}
			$sMulitPage .= '<td>转到第<input type="text" style="width:20px" id="page">页</td></tr></table></form>';
			$sMulitPage .= '<script>
			function mulitpagesub(obj) {
				obj.action += "&page="+document.getElementById("page").value;
			}
			</script>';
		} else {
			$sMulitPage = '';
		}
		return $sMulitPage;
	}
	
	public function sendPm($iUId, $sSubject, $sMessage) {
		$sTablePre = Base::getConf ( 'db.bbs' );
		$sSql = 'INSERT INTO ' . $sTablePre . 'promptmsgs (typeid, uid, new, dateline, message) VALUES (4, ?, 1, ?, ?)';
		$sMessage = '<div>' . $sSubject . ' {time}<br />' . nl2br ( $sMessage ) . '</div>';
		Base::$oBase->Db_exec ( $sSql, array ($iUId, time (), $sMessage ) );
		$sSql = 'SELECT COUNT(*) FROM ' . $sTablePre . 'promptmsgs WHERE typeid = 4 AND uid = ? AND new = 1';
		$iNum = Base::$oBase->Db_getOne ( $sSql, $iUId );
		$sSql = 'REPLACE INTO ' . $sTablePre . 'prompt (uid, typeid, number) VALUES (?, 4, ?)';
		Base::$oBase->Db_exec ( $sSql, array ($iUId, $iNum ) );
		$sSql = 'UPDATE ' . $sTablePre . 'members SET prompt=prompt|1 WHERE uid=?';
		Base::$oBase->Db_exec ( $sSql, $iUId );
	}
	
	public function clearCache($sName) {
		$sCache = Base::getConf ( 'cache.path' ) ? Base::getConf ( 'cache.path' ) : 'cache';
		$sTemplate = Base::getConf ( 'template.tmp_path' ) ? Base::getConf ( 'template.tmp_path' ) : $sCache . '/template';
		$sImage = Base::getConf ( 'image.upload_path' ) ? Base::getConf ( 'image.upload_path' ) : $sCache . '/image';
		$sUser = $sCache . '/user';
		$aPath ['data'] = realpath ( $sCache );
		$aPath ['template'] = realpath ( $sTemplate );
		$aPath ['image'] = realpath ( $sImage );
		$aPath ['user'] = realpath ( $sUser );
		if (! array_key_exists ( $sName, $aPath ) || $aPath [$sName] == false) {
			return false;
		}
		$aFile = Base::$oBase->File_ls ( $aPath [$sName] );
		foreach ( $aFile as $sFile ) {
			if (is_dir ( $sFile ) && in_array ( $sFile, $aPath )) {
				continue;
			} else {
				Base::$oBase->File_rm ( $sFile );
			}
		}
	
	}
}
?>
